package com.asiainfo.lop.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.toolbox.StringRequest;
import com.asiainfo.lop.BaseApplication;
import com.asiainfo.lop.os.InitParam;

import java.util.Map;

/**
 * Created by wucm(wucm@asiainfo.com) on 2016/5/28 20:10.
 */
public class VolleyRequest {
    public static StringRequest stringRequest;
    public static Context content;

    public static void get(Context mContent, String url, String tag, VolleyInterface volleyInterface) {
        Log.d(InitParam.TAG, "get: " + url);
        if (!isNetworkAvailable(mContent)) {
            Toast.makeText(mContent, "当前网络不可用，请设置网络", Toast.LENGTH_SHORT).show();
            return;
        }

        BaseApplication.getHttpQueues().cancelAll(tag);
        stringRequest = new StringRequest(Request.Method.GET, url, volleyInterface.getListener(), volleyInterface.getErrorListener());
        stringRequest.setTag(tag);
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        BaseApplication.getHttpQueues().add(stringRequest);
//        BaseApplication.getHttpQueues().start();
    }

    public static void post(Context mContent, String url, String tag, final Map paramMap, VolleyInterface volleyInterface) {
        Log.d(InitParam.TAG, "post: " + url);
        if (!isNetworkAvailable(mContent)) {
            Toast.makeText(mContent, "当前网络不可用，请设置网络", Toast.LENGTH_SHORT).show();
            return;
        }

        BaseApplication.getHttpQueues().cancelAll(tag);
        stringRequest = new StringRequest(Request.Method.POST, url, volleyInterface.getListener(), volleyInterface.getErrorListener()) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return paramMap;
            }
        };
        stringRequest.setTag(tag);
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        BaseApplication.getHttpQueues().add(stringRequest);
//        BaseApplication.getHttpQueues().start();
    }

    /**
     * 检查当前网络是否可用
     *
     * @return boolean
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null) {
            return false;
        } else {
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
